package ntut.andengine.physics;

import ntut.andengine.game.GameLevel;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class TransmissionGate extends PhysicalComponent {

	GameLevel mLevel;
	AnimatedSprite gateSprite;
	
	public TransmissionGate(GameLevel level) {
		super(level);
		mLevel = level;
		
		this.mBitmapTexture = new BitmapTexture(32, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TiledTextureRegion gateTextureRegion = BitmapTextureRegionFactory.createTiledFromAsset(this.mBitmapTexture, level, "gate_tiled.png", 0, 0, 1, 3);
		level.getEngine().getTextureManager().loadTexture(this.mBitmapTexture);			
		gateSprite = new AnimatedSprite(0, 0, gateTextureRegion);
		gateSprite.animate(new long[]{100, 100, 100}, 0, 2, 1);
		
		FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		fixtureDef.isSensor = true;
		mBody = PhysicsFactory.createBoxBody(level.getPhysicsWorld(), gateSprite, BodyType.StaticBody, fixtureDef);
		
		level.getScene().attachChild(gateSprite);
		level.getPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(gateSprite, mBody, true, true));
	}

}
